package com.hsd.znsh.controller.root;

import com.hsd.znsh.entity.Function;
import com.hsd.znsh.entity.User;
import com.hsd.znsh.enums.UserRoleEnum;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.pojo.output.TreeNode;
import com.hsd.znsh.repository.FunctionRepository;
import com.hsd.znsh.repository.RoleFunctionRepository;
import com.hsd.znsh.repository.UserRepository;
import com.hsd.znsh.repository.UserRoleRepository;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.SecurityUtil;
import com.hsd.znsh.utils.TreeUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-07 18:21:45
 */
@RequestMapping("/root/function")
@Controller
@Transactional
public class RootFunctionController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserRoleRepository userRoleRepository;

    @Autowired
    private RoleFunctionRepository roleFunctionRepository;

    @Autowired
    private FunctionRepository functionRepository;

    @GetMapping("/functionList")
    public String toFunctionList(){
        return "manager/function/function_tree";
    }


    @GetMapping("/icon/select")
    public String toIconSelect(String iconName, HttpServletRequest request){
        request.setAttribute("iconName", iconName);
        return "manager/icon/icon_selector";
    }



    @GetMapping("/getFunctionList/{roleId}")
    @ResponseBody
    public List<Integer> getFunctionListByRoleId(@PathVariable("roleId") Integer roleId){
        List<Integer> functionIds=roleFunctionRepository.findFunctionIdListByRoleId(roleId);
        if(functionIds==null) functionIds=new ArrayList<>();
        return functionRepository.findByIdListAndUrlIsNotNull(functionIds);
    }

    @GetMapping(value = "/treeData")
    @ResponseBody
    public List<TreeNode> getTreeData() {
        Sort sort=new Sort("bh");
        List<Function> funcs = functionRepository.findAll(sort);
        Map<Integer, TreeNode> nodelist = new LinkedHashMap<Integer, TreeNode>();
        for (Function func : funcs) {
            TreeNode node = new TreeNode();
            node.setText(func.getName());
            node.setId(func.getId());
            node.setParentId(func.getParentId());
            node.setBh(func.getBh());
            node.setIcon(func.getIcon());
            nodelist.put(node.getId(), node);
        }
        // 构造树形结构
        return TreeUtil.getNodeList(nodelist);
    }

    @GetMapping(value = "/get/{id}")
    @ResponseBody
    public Function get(@PathVariable("id") Integer id) {
        Function function = functionRepository.findOne(id);
        if (function.getParentId()!=null) {
            function.setParentName(functionRepository.findOne(function.getParentId()).getName());
        } else {
            function.setParentName("系统菜单");
        }
        return function;
    }

    @PostMapping(value = "/delete/{id}")
    @ResponseBody
    public ResultVO delete(@PathVariable("id") Integer id) {
        roleFunctionRepository.deleteByFunctionId(id);
        functionRepository.delete(id);
        return ResultVOUtil.success("删除成功");
    }

    @PostMapping(value = "/save")
    @ResponseBody
    public ResultVO save(@Valid Function function, BindingResult bindingResult) {
        ValidatorUtil.valid(bindingResult);
        functionRepository.save(function);
        return ResultVOUtil.success("保存成功");
    }


    @GetMapping(value = "getBackgroundManagerList")
    @ResponseBody
    public List<User> getBackgroundManagerList(String username) {
        return userRepository.findByUsernameLikeAndUserType("%"+username+"%",UserRoleEnum.BACKGROUND_MANAGER.getCode());
    }
}
